/*
 * @lc app=leetcode.cn id=309 lang=swift
 *
 * [309] 最佳买卖股票时机含冷冻期
 */

// @lc code=start
class Solution {
    func maxProfit(_ prices: [Int]) -> Int {
        let n = prices.count
        if n < 2 { return 0 }
        var dp = Array(repeating: Array(repeating: 0, count: 2), count: n)
        for i in 0..<n {
            if i == 0 {
                dp[i][0] = 0
                dp[i][1] = -prices[i]
                continue
            }
            if i == 1 {
                dp[i][0] = max(dp[i-1][0], dp[i-1][1]+prices[i])
                dp[i][1] = max(dp[0][1], dp[i-1][0]-prices[1])
                continue
            }
            dp[i][0] = max(dp[i-1][0], dp[i-1][1]+prices[i])
            dp[i][1] = max(dp[i-1][1], dp[i-2][0]-prices[i])
        }
        return dp[n-1][0]
    }
}
// @lc code=end

